package cn.dgiot.fd.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.dgiot.fd.dao.IPLCWarnReportDAO;
import cn.dgiot.fd.model.DeviceStateModel;
import cn.dgiot.fd.model.PLCWarnReportModel;
import cn.dgiot.fd.service.IDeviceStateService;
import cn.dgiot.fd.service.IPLCWarnReportService;

/**
 * [下位机告警流水]
 * @author
 *
 */
@Service
public class PLCWarnReportServiceImpl implements IPLCWarnReportService{
 
	@Autowired
	private IPLCWarnReportDAO plcWarnReportDAO;

	@Autowired
	private IDeviceStateService deviceStateService;
	
	@Override
	public void insertPLCWarnReport(PLCWarnReportModel plcWarnReportModel) {
		List<PLCWarnReportModel> plcWarnModelList = new ArrayList<PLCWarnReportModel>();
		String deviceName = plcWarnReportModel.getDeviceName();
		DeviceStateModel deviceStateModel = new DeviceStateModel();
    	deviceStateModel.setDeviceName(deviceName);
    	deviceStateModel.setProductKey(plcWarnReportModel.getProductKey());
    	DeviceStateModel deviceStateModelResult = deviceStateService.getDeviceState(deviceStateModel);
		PLCWarnReportModel param = new PLCWarnReportModel();
		param.setIotId(deviceStateModelResult.getIotId());
		param.setDeviceName(deviceName);
		param.setProductKey(plcWarnReportModel.getProductKey());
		param.setWtype(plcWarnReportModel.getWtype());
		param.setVal(plcWarnReportModel.getVal());
		param.setStatus(plcWarnReportModel.getStatus());
		param.setCreateTime(plcWarnReportModel.getCreateTime());
		param.setId(UUID.randomUUID().toString().replace("-", ""));
		param.setRequestId(plcWarnReportModel.getRequestId());
		param.setDpno(plcWarnReportModel.getDpno());
		plcWarnModelList.add(param);
		plcWarnReportDAO.insertPLCWarnReport(plcWarnModelList);
	}

	@Override
	public void updatePLCWarnReportStatus(PLCWarnReportModel plcWarnReportModel) {
		// TODO Auto-generated method stub
		plcWarnReportDAO.updatePLCWarnReportStatus(plcWarnReportModel);
	}

	@Override
	public List<PLCWarnReportModel> getPLCWarnInfo(String productKey,String deviceName,String dpno,String requestId) {
		PLCWarnReportModel plcWarnReportModel = new PLCWarnReportModel();
		plcWarnReportModel.setProductKey(productKey);
		plcWarnReportModel.setDeviceName(deviceName);
		plcWarnReportModel.setDpno(dpno);
		plcWarnReportModel.setRequestId(requestId);
		return plcWarnReportDAO.getPLCWarnInfo(plcWarnReportModel);
	}

}
